package com

import (
	"reflect"
)

func StructToMap(obj interface{}) map[string]interface{} {
	var vv reflect.Value
	var vt reflect.Type

	vt = reflect.TypeOf(obj)
	if vt.Kind() == reflect.Ptr {
		vv = reflect.ValueOf(obj).Elem()
		vt = vv.Type()
	}

	if vt.Kind() != reflect.Struct {
		panic("must be struct")
	}

	m := make(map[string]interface{})
	for i := 0; i < vt.NumField(); i++ {
		f := vt.Field(i)
		m[f.Name] = vv.FieldByIndex(f.Index).Interface()
	}

	return m
}
